package com.example.gobang_system.common.pojo;

import com.example.gobang_system.common.errorcode.ErrorCode;
import com.example.gobang_system.common.errorcode.GlobalErrorCodeConstants;
import lombok.Data;
import org.springframework.util.Assert;

import java.io.Serializable;

@Data
public class CommonResult<T> implements Serializable {
    /**
     * 错误码
     * @see ErrorCode#getCode()
     */
    private Integer code;
    /**
     * 返回数据
     */
    private T data;
    /**
     * 错误描述信息
     */
    private String errorMessage;

    /**
     * 业务处理成功
     * @param data
     * @return
     * @param <T>
     */
    public static <T> CommonResult<T> success(T data) {
        CommonResult result = new CommonResult();
        result.code = GlobalErrorCodeConstants.SUCCESS.getCode();
        result.data = data;
        result.errorMessage = "";
        return result;
    }

    /**
     * 业务处理失败
     * @param errorCode
     * @return
     * @param <T>
     */
    public static <T> CommonResult<T> fail(ErrorCode errorCode) {
        return fail(errorCode.getCode(), errorCode.getMessage());
    }

    /**
     * 业务处理失败
     * @param code
     * @param errorMessage
     * @return
     * @param <T>
     */
    public static <T> CommonResult<T> fail(Integer code, String errorMessage) {
        Assert.isTrue(!GlobalErrorCodeConstants.SUCCESS.getCode().equals(code),
                "code = 200, 运行成功");
        CommonResult result = new CommonResult();
        result.code = code;
        result.errorMessage = errorMessage;
        return result;
    }

    /**
     * 用户未登录
     */
    public static <T> CommonResult<T> noLogin() {
        CommonResult result = new CommonResult();
        result.code = 401;
        result.errorMessage = "用户未登录";
        return result;
    }

    /**
     * 用户多开
     * @return
     * @param <T>
     */
    public static <T> CommonResult<T> repeatConnection() {
        CommonResult result = new CommonResult();
        result.code = 402;
        result.errorMessage = "禁止多开游戏界面！";
        return result;
    }
}
